<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<meta http-equiv="pragma" content="no-cache" />
	<link rel="stylesheet" href="/css/style.css" type="text/css"/>
	<script type="text/javascript" src="/js/jquery.js"></script>
	<script type="text/javascript" src="/js/util.js"></script>
</head>
<script type="text/javascript">

var arguments = window.dialogArguments;
var elementPage = arguments[0];
var elementsstr = arguments[1];
window.resultValue = elementsstr;
var elements = [];
var count = 0;
if(elementsstr != undefined && elementsstr != "") {
	elements = s2json(elementsstr);
	count = elements.length;
}
$(document).ready(function() {
	var slices = "";
	for(var i=0;i<count;i++){
		var element = elements[i];
		slices = slices + getSlice(i,element);
	}
	$("#lists").html(slices);
});
function getSlice(i,element) {
	var slice = "<div id='slice"+ i +"'>";
	slice = slice + "<input type='text' id='element"+ i +"' name='element"+ i + "' value='" + json2s(element) + "' disabled/>";
	slice = slice + "<input type='button' value='...' onclick='editElement("+ i +")'/>";
	slice = slice + "<input type='button' value='删除' onclick='deleteElement("+ i +")'/>";
	slice = slice + "</div>";
	return slice;
}
function addElement() {
	var returnCallback = function(elementstr) {
		if(elementstr == null) {return;}
		var element = s2json(elementstr);
		slice = getSlice(count,element);
		$('#lists').html($('#lists').html() + slice);
		elements[count] = element;
		count = count + 1;
	}
	openDialog(elementPage,null,"dialogWidth:600px;dialogHeight:600px;status:no;help:no;center:yes",returnCallback);
}
function editElement(id) {
	var element = elements[id];
	var returnCallback = function(elementstr) {
		if(elementstr == null) {return;}
		var element = s2json(elementstr);
		elements[id] = element;
		$("#element" + id).val(elementstr);
	}
	openDialog(elementPage,json2s(element),"dialogWidth:600px;dialogHeight:600px;status:no;help:no;center:yes",returnCallback);
}
function deleteElement(id) {
	$('#slice' + id).hide();
	elements[id] = null;
}
function clearElements() {
	elements = []
	count = 0;
	$("#lists").html("");
}
function submitList() {
	var finalElements = [];
	for(var i=0;i<elements.length;i++) {
		if(elements[i] != undefined) {
			finalElements.push(elements[i]);
		}
	}
	window.returnValue = json2s(finalElements);
	beforeWindowClose();
	window.close();
}
function beforeWindowClose() {
	if(window.returnCallback) {
		window.returnCallback(window.returnValue);
	}
	window.onbeforeunload = null;
	return true;
}
</script>

<body onunload="beforeWindowClose()">
	<div id="lists">
		
	</div>
	<input type="button" onclick="addElement()" value="增加元素"/>
	<input type="button" onclick="clearElements()" value="清空元素"/>
	<input type="button" onclick="submitList()" value = "提交"/>
</body>